package me.firstapp.spi.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.system.JsonApiClient;
import me.firstapp.common.module.system.JsonApiClientRecord;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.system.ApiClientService;

@Service
public class ApiClientServiceImpl extends BaseServiceImpl implements ApiClientService {

	@Override
	public SingleObject<JsonApiClient> addApiClient(String email, String description) {
		SingleObject<JsonApiClient> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("email", email);
			parameters.put("description", description);

			JSONObject resultJson = api(parameters, "/security/addApiClient.do", HttpMethod.POST, "新增ApiClient");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonApiClient client = JSONObject.toJavaObject(resultJson.getJSONObject("object"), JsonApiClient.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(client);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonApiClient> updateApiClient(Long id, Long yesCounts, String description, Integer status) {
		SingleObject<JsonApiClient> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			parameters.put("yesCounts", yesCounts);
			parameters.put("status", status);
			parameters.put("description", description);

			JSONObject resultJson = api(parameters, "/security/updateApiClient.do", HttpMethod.POST, "更新ApiClient");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonApiClient client = JSONObject.toJavaObject(resultJson.getJSONObject("object"), JsonApiClient.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(client);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonApiClient> findApiClientPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonApiClient> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/security/findApiClientPage.do", "分页查询ApiClient",
					searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonApiClient> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonApiClient> clientList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonApiClient client = JSONObject.toJavaObject(_jsonObject, JsonApiClient.class);
								clientList.add(client);
							}
						}
					}
					jsonPage.setList(clientList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public ListObject<JsonApiClient> findAll() {
		ListObject<JsonApiClient> result = new ListObject<JsonApiClient>();
		Map<String, Object> parameters = new HashMap<>();
		JSONObject jsonObject = api(parameters, "/security/findAll.do", HttpMethod.GET, "获取全部ApiClient");

		try {
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONArray jsonArray = jsonObject.getJSONArray("items");
				List<JsonApiClient> clients = new ArrayList<JsonApiClient>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject _jsonObject = jsonArray.getJSONObject(i);
						if (_jsonObject != null) {
							JsonApiClient client = JSONObject.toJavaObject(_jsonObject, JsonApiClient.class);
							clients.add(client);
						}
					}
				}
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setItems(clients);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonApiClientRecord> addApiClientRecord(Long apiClientId, Long dateTime, Long counts) {
		SingleObject<JsonApiClientRecord> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("apiClientId", apiClientId);
			parameters.put("dateTime", dateTime);
			parameters.put("counts", counts);

			JSONObject resultJson = api(parameters, "/security/addApiClientRecord.do", HttpMethod.POST, "新增ApiClient");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonApiClientRecord record = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonApiClientRecord.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(record);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Long> findApiClientRecordTotalTimes(Long apiClientId) {
		SingleObject<Long> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("apiClientId", apiClientId);

			JSONObject resultJson = api(parameters, "/security/findApiClientRecordTotalTimes.do", HttpMethod.GET,
					"查询apiClient总共调用次数");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				Long counts = resultJson.getLong("object");
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(counts);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Long> findApiClientRecordMeanTimes(Long apiClientId) {
		SingleObject<Long> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("apiClientId", apiClientId);

			JSONObject resultJson = api(parameters, "/security/findApiClientRecordMeanTimes.do", HttpMethod.GET,
					"查询apiClient评均调用次数");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				Long counts = resultJson.getLong("object");
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(counts);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonApiClientRecord> findJsonApiClientRecordPage(Long apiClientId, Integer pageNo,
			Integer pageSize, Map<String, Object> searchParams, String sortParam, String sortType, Long startTime, Long endTime) {
		PageObject<JsonApiClientRecord> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			parameters.put("apiClientId", apiClientId);
			parameters.put("startTime", startTime);
			parameters.put("endTime", endTime);
			JSONObject jsonObject = apiBody(parameters, "/security/findApiClientRecordPage.do", "分页查询ApiClient记录",
					searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonApiClientRecord> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonApiClientRecord> recordList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonApiClientRecord record = JSONObject.toJavaObject(_jsonObject,
										JsonApiClientRecord.class);
								recordList.add(record);
							}
						}
					}
					jsonPage.setList(recordList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
